package codeTop;

import java.util.LinkedList;
import java.util.Objects;
import java.util.Stack;

public class q394 {
    public String decodeString(String s) {
        Stack<String> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != ']') {
                stack.push(s.substring(i,i+1));
            }else {
                StringBuilder sb = new StringBuilder();
                while (!Objects.equals(stack.peek(), "[")) {
                    sb.insert(0,stack.pop());
                }
                stack.pop();
                int num = 0;
                int time = 0;
                while (!stack.isEmpty()&&stack.peek().charAt(0) >= '0' && stack.peek().charAt(0) <= '9') {
                    num+= ((stack.pop().charAt(0)-'0')*(int)Math.pow(10,time++));
                }
                stack.push(sb.toString().repeat(num));
            }
        }
        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty()) {
            sb.insert(0,stack.pop());
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        q394 q = new q394();
        q.decodeString("100[leetcode]");
    }
}
